package com.jule.miaozhuapc.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.jule.miaozhuapc.dao.GameRecordDao;
import com.jule.miaozhuapc.domain.WawaGameRecord;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Repository
public class GameRecordDAOImpl implements GameRecordDao {

    private EntityManager em;

    @PersistenceContext
    public void setEm(EntityManager em) {
        this.em = em;
    }

    @Override
    public List<WawaGameRecord> findAllByUserId(Long userId, boolean type, int num, int size) {
        String hql = "from WawaGameRecord where userId =:userId and isDelete = 0  and isSuccess =:isDelete";
        List<WawaGameRecord> wawaGameRecord = em.createQuery(hql)
                .setParameter("userId", userId)
                .setParameter("isDelete",type)
                .setFirstResult(num - 1)
                .setMaxResults(size*num)
                .getResultList();
        return wawaGameRecord;
    }

    @Override
    public void update(WawaGameRecord gameRecord) {
        // TODO Auto-generated method stub
        em.persist(gameRecord);
    }


    @Override
    public WawaGameRecord findById(Long id) {
        String hql = "from  WawaGameRecord where id = ? and isDelete = 0  ";
        return em.find(WawaGameRecord.class, id);
    }

}
